<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class VideoCallAuditConfig extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'video_call_audit_config';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * @return false|\Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object
     */
    public function isEnableAudit()
    {
        $config = self::where(['flag' => 1])->orderBy('id', 'desc')->first();
        if (empty($config)) {
            return false;
        }

        if (!(date('H:s') >= $config->begin_time && date('H:s') <= $config->end_time)) {
            return false;
        }

        return $config;
    }

    /**
     * @param int $userId
     * @return array|null
     */
    public function getConfig($userId)
    {
        $config = self::where(['flag' => 1])->orderBy('id', 'desc')->first();
        if (empty($config)) {
            return null;
        }

        $enable = false;
        if (date('H:s') >= $config->begin_time && date('H:s') <= $config->end_time) {
            $enable = true;
        }

        $user = User::find($userId);
        if ($user) {
            if (!strstr($config->channel, $user->cur_channel)) {
                $enable = false;
            }

            if (!empty($config->version) && !strstr($config->version, $user->version)) {
                $enable = false;
            }
        }

        return [
            'frequency' => $config->frequency,
            'end_second' => $config->end_second,
            'enable' => $enable,
        ];
    }
}
